<?
class Data {

	var $data = array();

	function Data($data = null, $type = "rss"){

		if(!$data) return;

		switch ($type) {
			case "rss" :
				$this->fetch_rss($data);
				break;
			case "object" : 
				$this->fetch_object($data);
		}
	}

	public function add_data($data){
		$this->data = $data;
	}

	private function fetch_rss($rss){

		while($line = mysql_fetch_object($rss)){

			$tmp = null;

			foreach($line as $key => $value){
				$tmp[$key] = $value;
			}

			array_push($this->data, $tmp);
		}
	}

	function fetch_unique($field = ""){
		foreach($this->data as $row => $col){ 
			foreach($col as $key => $value){
				$tmp[$key] = $value;
			}
			break;
		}

		if($field != ""){
			return $tmp[$field];
		}

		return $tmp;
	}

	private function fetch_object($data) {
		foreach($data as $k => $v){
			$result[$k] = $v;
		}

		$this->data = $result;
	}

	public function category($field, $data){
		global $tpl, $dsfm_selcat;

		$tpl->assign("dsfm_open_category", true);
		$tpl->assign("dsfm_category_data", $data);
		$tpl->assign("dsfm_selcat", $dsfm_selcat);

		if($dsfm_selcat == "") return;

		foreach($this->data as $key => $row){
			if($row[$field] == $dsfm_selcat){
				$result[] = $row;
			}
		}

		$this->data = $result;
	}

	public function category_text($text){
		global $tpl;
		$tpl->assign("dsfm_category_text", $text);
	}

	public function search($field = ""){
		global $tpl, $dsfm_keyword;

		$tpl->assign("dsfm_open_search", true);

		if($dsfm_keyword == "")
			return;

		$tpl->assign("dsfm_keyword", $dsfm_keyword);

		$result = array();

		if(is_array($field)){
			foreach($this->data as $key => $row){
				foreach($field as $k => $col){
					if( strstr(strtolower($row[$col]), strtolower($dsfm_keyword)) ){
						$result[] = $row;
					}
				}
			}
		}elseif($field != ""){
			foreach($this->data as $key => $row){
				if( strstr(strtolower($row[$field]), strtolower($dsfm_keyword)) ){
					$result[] = $row;
				}
			}
		}else{
			foreach($this->data as $key => $row){
				foreach($row as $col => $value){
					if( strstr(strtolower($value), strtolower($dsfm_keyword)) ){
						$result[] = $row;
					}
				}
			}
		}

		$tpl->assign("dsfm_search_link", "&amp;dsfm_keyword=$dsfm_keyword");

		$this->data = $result;
	}

	public function search_text($text){
		global $tpl;
		$tpl->assign("dsfm_search_text", $text);
	}

	public function filter($field){
		global $tpl, $dsfm_from, $dsfm_to, $dsfm_module;

		$tpl->assign("dsfm_open_filter", true);

		$dsfm_from = trim($dsfm_from);
		$dsfm_to = trim($dsfm_to);

		if($dsfm_from == "" && $dsfm_to == "")
			return;

		$tpl->assign("dsfm_from", $dsfm_from);
		$tpl->assign("dsfm_to", $dsfm_to);

		$result = array();
		$arg = "";

		if($dsfm_from != ""){
			foreach($this->data as $key => $row){
				if( strtotime($row[$field]) >= strtotime($dsfm_from) ){
					$result[] = $row;
				}
			}

			$arg .= "&amp;dsfm_from=$dsfm_from";

		}elseif($dsfm_to != ""){
			foreach($this->data as $key => $row){
				if( strtotime($row[$field]) <= strtotime($dsfm_to) )
					$result[] = $row;
			}

			$arg .= "&amp;dsfm_to=$dsfm_to";

		}else{
			foreach($this->data as $key => $row){
				if(strtotime($row[$field]) >= strtotime($dsfm_from) && strtotime($row[$field]) <= strtotime($dsfm_to))
					$result[] = $row;
			}

			$arg .= "&amp;dsfm_from=$dsfm_from&amp;dsfm_to=$dsfm_to";
		}

		$tpl->assign("dsfm_filter_link", $arg);

		$this->data = $result;
	}

	public function fetch_option($k, $v){
		foreach($this->data as $row => $col){
			$tmp[$col[$k]] = $col[$v];
		}

		return $tmp;
	}

	public function fetch(){
		return $this->data;
	}
}
?>